using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MsgManager<T> : Singleton<MsgManager<T>>
{
    Dictionary<int,Action<T>> msgDic = new Dictionary<int,Action<T>>();

    public void AddListener(int id, Action<T> callback)
    {
        if (msgDic.ContainsKey(id))
        {
            msgDic[id] += callback;
        }
        else
        {
            msgDic.Add(id,callback);
        }
    }

    public void RemoveListener(int id, Action<T> callback)
    {
        if (msgDic.ContainsKey(id))
        {
            msgDic[id] -= callback;
            if (msgDic[id]==null)
            {
                msgDic.Remove(id);
            }
        }
    }

    public void Broadcast(int id, T t)
    {
        if (msgDic.ContainsKey(id))
        {
            msgDic[id]?.Invoke(t);
        }
    }
    
    
}
